package com.controller;

import com.entity.MaintenanceDailyWorkRecord;
import com.service.MaintenanceDailyWorkRecordService;
import com.dto.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/api/maintenance/work/daily")
public class MaintenanceDailyWorkRecordController {

    @Autowired
    private MaintenanceDailyWorkRecordService maintenanceDailyWorkRecordService;

    @PostMapping
    public MaintenanceDailyWorkRecord create(@RequestBody MaintenanceDailyWorkRecord maintenanceDailyWorkRecord) {
        return maintenanceDailyWorkRecordService.save(maintenanceDailyWorkRecord);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable(value = "id") Integer id) {
        maintenanceDailyWorkRecordService.deleteById(id);
    }

    @PutMapping("/{id}")
    public MaintenanceDailyWorkRecord update(@PathVariable(value = "id") Integer id, @RequestBody MaintenanceDailyWorkRecord maintenanceDailyWorkRecord) {
        maintenanceDailyWorkRecord.setId(id);
        return maintenanceDailyWorkRecordService.save(maintenanceDailyWorkRecord);
    }

    @GetMapping("/{id}")
    public MaintenanceDailyWorkRecord detail(@PathVariable(value = "id") Integer id) {
        return maintenanceDailyWorkRecordService.findById(id).orElse(null);
    }

    @GetMapping("/page")
    public Page<MaintenanceDailyWorkRecord> page(@RequestParam(defaultValue = "1") int pageNum,
                       @RequestParam(defaultValue = "10") int pageSize,
                       @RequestParam(required = false) String workCategory) {
        Page<MaintenanceDailyWorkRecord> page;
        if (workCategory != null && !workCategory.isEmpty()) {
            page = maintenanceDailyWorkRecordService.findByWorkCategoryContaining(workCategory, pageNum, pageSize);
        } else {
            page = maintenanceDailyWorkRecordService.findAll(pageNum, pageSize);
        }
        return page;
    }
}